package cn.bytecollege.user.filter;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.*;
import cn.bytecollege.user.service.OrderDetailService;
import cn.bytecollege.user.service.OrdersService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@WebFilter(filterName = "OrderDetailFilter",servletNames = {"OrderDetailAddUpdateController","OrderDetailUpdateController"})
public class OrderDetailFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        ProductService productService=new ProductService();
        String method =request.getMethod();
        String uri=request.getRequestURI();

            if(method.equalsIgnoreCase("get")){
              if(uri.contains("Update")){
                OrderDetailService orderDetailService=new OrderDetailService();
                String update=request.getParameter("update");
                String orderDetailId=request.getParameter("orderdetailid");
                OrderDetail orderDetail=orderDetailService.findById(Integer.valueOf(orderDetailId));
                Integer productId=orderDetail.getProductId();
                Integer productnumber=orderDetail.getProductNumber();
                Product product=productService.findById(productId);
                String msg="";
                if(update.equals("0")){           //0为减少,1为增加
                   if((productnumber-1)<=0){
                       msg="不能购买0件！";
                       request.setAttribute("msg",msg);
                       HttpSession session= request.getSession();
                       User user=(User) session.getAttribute("user");
                       Integer userId=user.getUserId();
                       List<OrderList> lists=orderDetailService.orderLists(userId);
                       for(int k=0;k<lists.size();k++){
                           if (lists.get(k).getProductParticulars().length()>15){
                               lists.get(k).setProductParticulars(lists.get(k).getProductParticulars().substring(0,15));
                           }
                       }
                       request.setAttribute("orderlist",lists);
                       Integer sumProduct=0;
                       BigDecimal sumOrders= BigDecimal.valueOf(0);
                       for(int k=0;k<lists.size();k++){
                           sumProduct+=lists.get(k).getProductNumber();
                           BigDecimal m=BigDecimal.valueOf(lists.get(k).getProductNumber());
                           BigDecimal n=lists.get(k).getProductPrice();
                           sumOrders = sumOrders.add(m.multiply(n));
                       }
                       request.setAttribute("sumProduct",sumProduct);
                       request.setAttribute("sumOrders",sumOrders);
                       request.getRequestDispatcher("/cart.jsp").forward(request,resp);
                       return;
                   }
                } else if(update.equals("1")){
                    if((productnumber+1)>product.getProductAllnumber()){
                        msg="超过库存";
                        request.setAttribute("msg",msg);
                        HttpSession session= request.getSession();
                        User user=(User) session.getAttribute("user");
                        Integer userId=user.getUserId();
                        List<OrderList> lists=orderDetailService.orderLists(userId);
                        for(int k=0;k<lists.size();k++){
                            if (lists.get(k).getProductParticulars().length()>15){
                                lists.get(k).setProductParticulars(lists.get(k).getProductParticulars().substring(0,15));
                            }
                        }
                        request.setAttribute("orderlist",lists);
                        Integer sumProduct=0;
                        BigDecimal sumOrders= BigDecimal.valueOf(0);
                        for(int k=0;k<lists.size();k++){
                            sumProduct+=lists.get(k).getProductNumber();
                            BigDecimal m=BigDecimal.valueOf(lists.get(k).getProductNumber());
                            BigDecimal n=lists.get(k).getProductPrice();
                            sumOrders = sumOrders.add(m.multiply(n));
                        }
                        request.setAttribute("sumProduct",sumProduct);
                        request.setAttribute("sumOrders",sumOrders);
                        request.getRequestDispatcher("/cart.jsp").forward(request,resp);
                        return;
                    }
                }
            }else if(uri.contains("addupdate")){
                  OrderDetailService orderDetailService=new OrderDetailService();
                  String update=request.getParameter("update");
                  String orderDetailId=request.getParameter("orderdetailid");
                  OrderDetail orderDetail=orderDetailService.findById(Integer.valueOf(orderDetailId));
                  Integer productId=orderDetail.getProductId();
                  Integer productnumber=orderDetail.getProductNumber();
                  if(productnumber==null){
                      productnumber=1;
                  }
                  Product product=productService.findById(productId);
                  String msg="";
                  if(update.equals("0")){           //0为减少,1为增加
                      if((productnumber-1)<=0){
                          msg="不能购买0件！";
                          request.setAttribute("msg",msg);
                          String id = request.getParameter("productid");
                          Integer productId1 = Integer.parseInt(id);
                          Product product1 = productService.findClassifyById(productId1);
                          request.setAttribute("product",product1);
                          List<Product> lists=productService.findAll();
                          request.setAttribute("lists",lists);
                          List<Product> linelists=null;
                          if(lists.size()>3){
                              linelists=lists.subList(0,3);
                          }else {
                              linelists=lists.subList(0,lists.size());
                          }
                          request.setAttribute("linelists",linelists);
                          orderDetail.setProductNumber(1);
                          request.setAttribute("orderDetail",orderDetail);
                          request.getRequestDispatcher("/productview.jsp").forward(request,resp);
                          return;
                      }
                  } else if(update.equals("1")){
                      if((productnumber+1)>product.getProductAllnumber()){
                          msg="超过库存";
                          request.setAttribute("msg",msg);
                          String id = request.getParameter("productid");
                          Integer productId1 = Integer.parseInt(id);
                          Product product1 = productService.findClassifyById(productId1);
                          request.setAttribute("product",product1);
                          List<Product> lists=productService.findAll();
                          request.setAttribute("lists",lists);
                          List<Product> linelists=null;
                          if(lists.size()>3){
                              linelists=lists.subList(0,3);
                          }else {
                              linelists=lists.subList(0,lists.size());
                          }
                          request.setAttribute("linelists",linelists);
                          request.setAttribute("orderDetail",orderDetail);
                          request.getRequestDispatcher("/productview.jsp").forward(request,resp);
                          return;
                      }
                  }

              }
            chain.doFilter(request,resp);
            return;
        }
        chain.doFilter(request,resp);

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
